package com.game.state;

import javax.microedition.khronos.opengles.GL10;

import media.sound.GameSound;

import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;
import system.Device;
import system.GameOption;
import system.Graphics;
import system.KeyBoard;

import com.game.GamePlay;
import com.game.R;

public class Option extends State {
	private static final int MENU_ITEM_SOUND = 0;
	private static final int MENU_ITEM_MUSIC = MENU_ITEM_SOUND + 1;
	private static final int MENU_ITEM_DONE = MENU_ITEM_MUSIC + 1;
	private static final int MENU_ITEM_BEGIN = MENU_ITEM_SOUND;
	private static final int MENU_ITEM_END = MENU_ITEM_DONE;

	private int selected = MENU_ITEM_BEGIN;

	private GL10 gl;
	private int m_fSrcWidth;
	private int m_fSrcHeight;
	private static int sNumMenuItem = 3;
	private int[] m_iMenuItemOnTex = new int[sNumMenuItem];
	private int[] m_iMenutemOnSelectedTex = new int[sNumMenuItem];
	private int[] m_iMenuItemOffTex = new int[sNumMenuItem];
	private int[] m_iMenutemOffSelectedTex = new int[sNumMenuItem];
	private boolean[] m_isItemOn = new boolean[sNumMenuItem];
	private int m_itexTitleID;
	private int m_itexBgID;

	private Texture m_texture = new Texture();
	private TexSquare m_optionItem;
	private TexSquare m_title;
	private int m_iOffset = 200;
	private TexSquare m_optionBg;

	@Override
	public void Init() {
		gl = Graphics.GetGraphics();
		m_fSrcWidth = Device.GetWidth();
		m_fSrcHeight = Device.GetHeight();

		m_iOffset = m_fSrcHeight / 4;

		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		float w = m_fSrcWidth / 2;
		float h = m_fSrcHeight / 2;
		gl.glOrthof(-w, w, -h, h, -1.0f, 1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		// Load menu item texture
		m_iMenuItemOnTex[MENU_ITEM_MUSIC] = m_texture
				.initTexture(R.drawable.mnu_opt_music_on);
		m_iMenutemOnSelectedTex[MENU_ITEM_MUSIC] = m_texture
				.initTexture(R.drawable.mnu_opt_music_on_select);
		m_iMenuItemOffTex[MENU_ITEM_MUSIC] = m_texture
				.initTexture(R.drawable.mnu_opt_music_off);
		m_iMenutemOffSelectedTex[MENU_ITEM_MUSIC] = m_texture
				.initTexture(R.drawable.mnu_opt_music_off_select);
		m_iMenuItemOnTex[MENU_ITEM_SOUND] = m_texture
				.initTexture(R.drawable.mnu_opt_sound_on);
		m_iMenutemOnSelectedTex[MENU_ITEM_SOUND] = m_texture
				.initTexture(R.drawable.mnu_opt_sound_on_select);
		m_iMenuItemOffTex[MENU_ITEM_SOUND] = m_texture
				.initTexture(R.drawable.mnu_opt_sound_off);
		m_iMenutemOffSelectedTex[MENU_ITEM_SOUND] = m_texture
				.initTexture(R.drawable.mnu_opt_sound_off_select);

		m_iMenuItemOnTex[MENU_ITEM_DONE] = m_texture
				.initTexture(R.drawable.mnu_done);
		m_iMenutemOnSelectedTex[MENU_ITEM_DONE] = m_texture
				.initTexture(R.drawable.mnu_done_select);
		
		m_optionItem = new TexSquare(Size.MNU_ITEM_WIDTH, Size.MNU_ITEM_HEIGHT);
		
		// Load background texture
		m_itexBgID = m_texture.initTexture(R.drawable.mainmenu_bg);
		m_optionBg = new TexSquare(Size.SCREEN_WIDTH, Size.SCREEN_HEIGHT);

		// Load title texture
		m_itexTitleID = m_texture.initTexture(R.drawable.mnu_opt_title);
		m_title = new TexSquare(m_texture.getWidth(), m_texture.getHeight());

		// set flag variable
		m_isItemOn[MENU_ITEM_MUSIC] = GameOption.isMusicOn;
		m_isItemOn[MENU_ITEM_SOUND] = GameOption.isSoundOn;
		m_isItemOn[MENU_ITEM_DONE] = true;
	}

	@Override
	public void Update(GamePlay gamePlay) {
		

		super.Update(gamePlay);
	}

	@Override
	public void Render() {
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		// Back ground
		m_texture.loadTexture(m_itexBgID);
		m_optionBg.Render();

		// Title
		m_texture.loadTexture(m_itexTitleID);
		gl.glPushMatrix();
		gl.glTranslatef(0, m_iOffset, 0);
		m_title.Render();
		gl.glPopMatrix();
		// Draw menuItem
		gl.glPushMatrix();
		for (int i = 0; i < sNumMenuItem; ++i) {
			if (m_isItemOn[i]) {
				if (selected == i)
					m_texture.loadTexture(m_iMenutemOnSelectedTex[i]);
				else
					m_texture.loadTexture(m_iMenuItemOnTex[i]);
			} else {
				if (selected == i)
					m_texture.loadTexture(m_iMenutemOffSelectedTex[i]);
				else
					m_texture.loadTexture(m_iMenuItemOffTex[i]);
			}
			gl.glPushMatrix();
			gl.glTranslatef(0, (sNumMenuItem / 2 - i - 1)
					* Device.MENU_BUTTON_HEIGHT, 0);
			m_optionItem.Render();
			gl.glPopMatrix();
		}
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

	}

	@Override
	public void Destroy() {
		m_iMenuItemOnTex = null;
		m_iMenutemOnSelectedTex = null;
		m_iMenuItemOffTex = null;
		m_iMenutemOffSelectedTex = null;
		m_isItemOn = null;

		m_texture = null;
		m_optionItem = null;
		m_title = null;
		m_optionBg = null;
	}

	@Override
	public void ProcessKeyboard(GamePlay gamePlay) {
		// TODO Auto-generated method stub
		if (KeyBoard.IsKeyPressed(DK_PAD_DOWN)) {
			selected++;
			if (selected > MENU_ITEM_END)
				selected = MENU_ITEM_END;
			
			if (GameOption.isSoundOn)
				GameSound.GetEffSound().PlayChangeMenu();
		} else if (KeyBoard.IsKeyPressed(DK_PAD_UP)) {
			selected--;
			if (selected < MENU_ITEM_BEGIN)
				selected = MENU_ITEM_BEGIN;
			
			if (GameOption.isSoundOn)
				GameSound.GetEffSound().PlayChangeMenu();
		} else if (KeyBoard.IsKeyPressed(DK_PAD_MIDDLE)) {
			switch (selected) {
			case MENU_ITEM_SOUND:
				m_isItemOn[MENU_ITEM_SOUND] = !m_isItemOn[MENU_ITEM_SOUND];
				
				if (GameOption.isSoundOn)
					GameSound.GetEffSound().PlayChangeMenu();
				break;
			case MENU_ITEM_MUSIC:
				m_isItemOn[MENU_ITEM_MUSIC] = !m_isItemOn[MENU_ITEM_MUSIC];
				
				if (GameOption.isSoundOn)
					GameSound.GetEffSound().PlayChangeMenu();
				break;
			case MENU_ITEM_DONE:
				if(GameOption.isMusicOn != m_isItemOn[MENU_ITEM_MUSIC]){
					GameOption.isMusicOn = m_isItemOn[MENU_ITEM_MUSIC];
					if(!GameOption.isMusicOn) GameSound.GetBgSound().Stop();
					else GameSound.GetBgSound().PlayMainMenu();
				}
				GameOption.isSoundOn = m_isItemOn[MENU_ITEM_SOUND];
				if(!gamePlay.ResumeState())
					gamePlay.SwitchState(GameState.MainMenu);
				
				if (GameOption.isSoundOn)
					GameSound.GetEffSound().PlaySelectMenu();
				break;
			}
		}
	}

}
